/**
 * dialog
 * 结构:
 * <div id=''>
 * 		<div class="dialog-close">close</div>
 * 		<div class="dialog-title">title</div>
 * 		<div class="dialog-content"></div>
 * 		...<input class='dialog-check' type='checkbox'  />
 * 		<div class="dialog-bottom">
 * 			<button class="dialog-submit">Submit</button>
 * 			<button class="dialog-cancel">Cancel</button>
 *     </div>
 * </div>
 * @param
 * options = {
 * 		submit: function(data){
 * 			//click submit button
 * 		},
 * 		close: function(){
 * 			//click close or cancel button
 * 		},
 * 		open: false,  //open the dailog the first time, or pass 'open' param to open later.
 * 		modal: true,
 * 		drag: true,
 * 		offsetLeft: -50,  
 * 		offsetTop: -50,  //offset relative to center
 *     gather: false  //gather input data for submit click
 * }
 * @author  tanggf
 */
(function($){
	$.fn.admindialog = function(options){		
		var defaultOpts = {
			open: false,
			modal: true,
			drag: true,
			offsetLeft: 0,
			offsetTop: -80,
			gatherData: false,
			submitDisableClass: 'ytb-button-disabled',
			modalClass: 'alert-opacity-bg',
			iframeOverlay: false,
			iframeClass: 'iframe-opacity',
			closeAfterSubmit: true
		};
		
		return this.each(function(){
			if(typeof options == 'object'){
				defaultOpts = $.extend(defaultOpts, options);
			}
			
			var dailog = _get_dailog_(this, defaultOpts);
			if(options == 'open'){
				dailog.open();
			}else if(options == 'close'){
				dailog.close();			
			}else if(options.open === true){
				dailog.open();
			}						
		});		
	};
	
	function _get_dailog_(ele, options){
		var dialog = $(ele).data("_wt_dialog_");
		if( ! dialog){
			dialog = new _Dialog_(ele, options);
			$(ele).data("_wt_dialog_", dialog);
		}
		return dialog;
	}
	
	function _Dialog_(ele, options){
		this.$ele = $(ele);
		this.options = options;
		this._init_();
	}
	
	_Dialog_.prototype = {
		_init_: function(){					
			this.$close = this.$ele.children("div.dialog-close");
			this.$bottom = this.$ele.find("div.dialog-bottom");
			this.$submit = this.$bottom.children("button.dialog-submit");			
			this.$cancel = this.$bottom.children("button.dialog-cancel");		
			this.$checks = this.$ele.find("input[type='checkbox'].dailog-check");		
			
			var self = this;			
			if(this.$checks.length > 0){				
				this._check_checkbox();
				this.$checks.click(function(){				
					self._check_checkbox();
				});
			}else{
				this.checked = true;
			}		
			
			this.$close.click(function(){
				self.close();
			});
			
			this.$cancel.click(function(){
				self.close();
			});
			
			this.$submit.click(function(){					
				if(self.options.submit && self.checked){
					var data = null;
					if(self.options.gatherData){
						data = self._gather_data();
					}					
					self.options.submit(data);			
				}
				
				if(self.checked && self.options.closeAfterSubmit){
					self.close();
				}				
			});
			
			if(this.options.modal){				
				this._pin_overlay_div();
			}
			
			//open the first time
			if(this.options.open){
				this.open();
			}
		},
		
		_check_checkbox: function(){
			this.checked = true;
			for ( var i = 0; i < this.$checks.length; i++) {
				var $cc = this.$checks.eq(i);
				if( ! $cc.attr("checked")){
					this.checked = false;
					break;
				}
			}					
			
			if(this.checked){					
				this.$submit.removeClass(this.options.submitDisableClass);
			}else{
				this.$submit.addClass(this.options.submitDisableClass);
			}
		},
		
		close: function(){
			this.$ele.hide();
			if(this.options.close){
				this.options.close();
			}
			if(this.options.modal){
				this.$overlay.hide();
				if(this.options.iframeOverlay){
					this.$iframe.hide();
				}				
			}
		},
		
		open: function(){
			var pos = this._cal_position();			
			this.$ele.css(pos).show();
			if(this.options.modal){
				var width = $(document).width();
				var height = $(document).height();		
				var wh = {'width': width, 'height': height};
				this.$overlay.css(wh).show();				
				if(this.options.iframeOverlay){				
					this.$iframe.css(wh).show();
				}
			}
		},
		
		_pin_overlay_div: function(){			
			if(this.options.iframeOverlay){
				this.$iframe = $("<iframe></iframe>").addClass(this.options.iframeClass).appendTo(document.body);	
			}					
			this.$overlay = $("<div />").addClass(this.options.modalClass).appendTo(document.body);			
		},
		
		_gather_data: function(){
			var $texts = this.$ele.find("input[type='text']");
			var $checkboxs = this.$ele.find("input[type='checkbox']");
			var $selects = this.$ele.find("select");
			var $areas = this.$ele.find("textarea");
			var $passwords = this.$ele.find("input[type='password']");
			var $hiddens = this.$ele.find("input[type='hidden']");
			var data = this._put_to_data([$texts, $selects, $areas, $passwords, $hiddens]);					
			$checkboxs.each(function(){
				var $this = $(this);
				var value = false;
				if($this.attr("checked")){
					 value = true;
				}
				data[$this.attr("name")] = value;
			});			
			
			return data;
		},
		
		_put_to_data: function(inputs){
			var data = {};
			for ( var i = 0; i < inputs.length; i++) {
				var $input = inputs[i];
				$input.each(function(){
					var $this = $(this);
					data[$this.attr('name')] = $this.val();
				});
			}
			return data;
		},
		
		_cal_position: function(){
			//居中
			$doc = $(document);			
			$win = $(window);
			var wHi = $win.height();
			var wWi = $win.width();
			var sHi = $doc.scrollTop();
			var sWi = $doc.scrollLeft();
			var wi = this.$ele.width();
			var hi = this.$ele.height();
			var ssWi = (wWi - wi) / 2 + sWi;
			var ssHi = (wHi - hi) / 2 + sHi;
			
			return {top: (ssHi + this.options.offsetTop),  left: (ssWi + this.options.offsetLeft)};			
		}
	};
})(jQuery);